Imports KanBan.Pgd.Infrastructure.Exceptions
Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Infrastructure.Repositories
Imports KanBan.Pgd.Presentation.Views

Public Class ManageGroups

    Private Const DefaulGridViewRowHeight As Integer = 36


    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.LoadGroupsGrid()

    End Sub

    Private Sub LoadGroupsGrid()

        Dim groupRepository As New GroupRepository()

        Dim groups As IEnumerable(Of Group) = groupRepository.GetAll()

        Me.AddGroups(groups)

    End Sub

#Region "IManageGroupsView"

    Public Sub AddGroups(ByVal groups As IEnumerable(Of Group))
        For Each g As Group In groups
            AddGroup(g)
        Next
    End Sub

    Public Sub AddGroup(ByVal group As Group)

        Dim row As New DataGridViewRow()
        row.CreateCells(grvGroups)
        row.Height = DefaulGridViewRowHeight

        row.Cells(GridColumns.Id).Value = group.Id
        row.Cells(GridColumns.Name).Value = group.Name
        row.Cells(GridColumns.UsersCount).Value = group.Users.Count

        grvGroups.Rows.Add(row)

    End Sub

    Public Sub UpdateGroup(ByVal group As Group)
        For Each r As DataGridViewRow In grvGroups.Rows
            If CType(r.Cells(GridColumns.Id).Value, Guid) = group.Id Then
                r.Cells(GridColumns.Name).Value = group.Name
                r.Cells(GridColumns.UsersCount).Value = group.Users.Count
                Exit For
            End If
        Next
    End Sub

#End Region

    Private Sub grvGroups_CellClick(ByVal sernder As Object, ByVal e As DataGridViewCellEventArgs) Handles grvGroups.CellClick

        If e.RowIndex >= 0 Then

            Dim groupId As Guid = CType(grvGroups.Rows(e.RowIndex).Cells(GridColumns.Id).Value, Guid)

            If e.ColumnIndex = GridColumns.Edit Then
                Me.EditGroup(groupId)
            End If

            If e.ColumnIndex = GridColumns.Delete Then
                If UIHelper.DeletionConfirmed("Are you sure you want to delete this Group?") Then
                    Me.DeleteGroup(groupId)
                    grvGroups.Rows.Remove(grvGroups.Rows(e.RowIndex))
                End If
            End If

            If e.ColumnIndex = GridColumns.Image Then
                Me.ShowGroup(groupId)
            End If

        End If

    End Sub

    Private Sub ShowGroup(ByVal groupId As Guid)

        Dim form As New ShowGroup(groupId)
        form.ShowDialog()

    End Sub

    Public Sub DeleteGroup(ByVal groupId As Guid)

        Dim groupRepository As New GroupRepository()

        groupRepository.DeleteLogical(groupId)

    End Sub

    Public Sub EditGroup(ByVal groupId As Guid)


        Dim form As New EditGroup(groupId)
        AddHandler form.GroupUpdated, AddressOf OnGroupUpdated

        form.ShowDialog()

    End Sub

    Private Sub OnGroupUpdated(ByVal updatedGroup As Group)

        Me.UpdateGroup(updatedGroup)

    End Sub

    Private Sub grvGroups_CellMouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grvGroups.CellMouseEnter
        If e.RowIndex >= 0 Then
            grvGroups.Rows(e.RowIndex).Selected = True

            If e.ColumnIndex = GridColumns.Image Or e.ColumnIndex = GridColumns.Edit Or e.ColumnIndex = GridColumns.Delete Then
                Me.Cursor = Cursors.Hand
            End If

        End If
    End Sub

    Private Sub grvGroups_CellMouseLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grvGroups.CellMouseLeave
        If e.RowIndex >= 0 Then
            grvGroups.Rows(e.RowIndex).Selected = False
            Me.Cursor = Cursors.Arrow
        End If
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Me.AddNewGroup()
    End Sub

    Public Sub AddNewGroup()

        Dim form As New EditGroup()
        AddHandler form.GroupAdded, AddressOf OnGroupAdded

        form.ShowDialog()

    End Sub

    Private Sub OnGroupAdded(ByVal addedGroup As Group)

        Me.AddGroup(addedGroup)

    End Sub

    Private Sub btnCloseTab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseTab.Click
        Me.Close()
    End Sub

    Private Enum GridColumns
        Image = 0
        Id = 1
        Name = 2
        UsersCount = 3
        Edit = 4
        Delete = 5
    End Enum

End Class